﻿using Framework.Attributes;
using Model.Base;
using Model.System.User;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;

namespace Service.System
{
    [Service]
    public class UserService
    {
        private readonly ISqlSugarClient client;

        public UserService(ISqlSugarClient client)
        {
            this.client = client;
        }
        /// <summary>
        /// 查询用户信息
        /// </summary>
        /// <returns></returns>
        public async Task<Page<sys_user>> SelectAsync(UserDto data)
        {
            Page<sys_user> result = new Page<sys_user>();
            var exp = Expressionable.Create<sys_user>();
            exp.AndIF(data.user_id > 0, it => it.user_id == data.user_id);
            exp.AndIF(!string.IsNullOrEmpty(data.user_name),it=>it.user_name == data.user_name);
            exp.AndIF(!string.IsNullOrEmpty(data.tel), it => it.tel == data.tel);
            exp.AndIF(!string.IsNullOrEmpty(data.email), it => it.email == data.email);
            exp.AndIF(!string.IsNullOrEmpty(data.status), it => it.status == data.status);
            exp.AndIF(!string.IsNullOrEmpty(data.delete_flag), it => it.delete_flag == data.delete_flag);

            var list = await client.Queryable<sys_user>().Where(exp.ToExpression()).ToPageListAsync(data.PageCount,data.PageSize, result.Total);
            result.PageSize = data.PageSize;
            result.PageCount = data.PageCount;
            result.Rows = list;
            return result;
        }

        /// <summary>
        /// 登录查询
        /// </summary>
        /// <param name="user_name">用户名</param>
        /// <returns></returns>
        public async Task<sys_user> SelectLoginAsync(string user_name)
        {
          return await client.Queryable<sys_user>().FirstAsync(it => it.user_name == user_name);
        }

    }
}
